//
//  MainViewController.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/21.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 在 viewDidLoad 只是添加子控制器，不会创建 tabBar 的 UITabBarButton
        // tabBar 的按钮们会在 viewWillAppear(将要显示) 方法之前被创建
        MayNewFeatureView()
        
        addChildViewControllers()
  
        setupTabBar()
    
    }
    //监听方法
    @objc private func clickComposeButton() {
        print("点击撰写按钮")
        
        var vcName: String
     //   print(NSBundle.mainBundle().infoDictionary) //CFBundleExecutable
        let namespace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
//        //判断用户是否登录
//        if !UserAccount.shardUserAccount.isLogin{
//            //显示访客视图
//            let vc = OAuthViewController()
//            
//            presentViewController(vc, animated: true, completion:nil)
//        }
//        //登录 显示composeViewController
//        let vc = composeViewController()
//        
//        let nav = UINavigationController(rootViewController: vc)
//        
//        presentViewController(nav, animated:true, completion:nil)
        
        
        //标准Swift 写法，class中需要包含命名空间
        if UserAccount.shardUserAccount.isLogin{
        
            vcName = "ComposeViewController" 
        }else{
            vcName = "OAuthViewController"
        }
//        let cls1:AnyObject = NSClassFromString(namespace + "." + vcName)!
//        
//        let cls = cls1 as! UIViewController.Type
    //   let cls = NSClassFromString(vcName + "." + "self") as! UIViewController.Type
        
        let cls = NSClassFromString(namespace + "." + vcName) as! UIViewController.Type
        let vc = cls.init()
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated:true, completion:nil)
    }

    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //将撰写按钮设置到顶部
        tabBar.bringSubviewToFront(composeButton)
        
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1

        //设置frame
        // >o 就是向内缩小
        // <0 就是向外
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)

    }
  
    private lazy var composeButton:UIButton = UIButton(cz_butitle:"", imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button", color: UIColor.orangeColor())
    
    
    //(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
    
    
}

extension MainViewController{

    private  func MayNewFeatureView(){
        
        //判断用户是否登录，没有登录的话就跳到RootViewController
//        if !UserAccount.shardUserAccount.isLogin{
//             return
//        }
//       
        let v = self.isEdition ? NewFeatureView() : WelcomeBackView()

        view.addSubview(v)
      
    }

}
//判断是否有新版本
extension MainViewController{
   
    private var isEdition:Bool{
       //检查当前版本
       let currentVersion = version
      // 获取之前的版本
        let versionKey = "hlc.com"
        let sanboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
      //将当前版本存到沙盒中
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
       
        
        print("是否是新版本 + \(currentVersion > sanboxVersion)")
        
        return currentVersion == sanboxVersion ? false : true
    }
    
    private var version:String{
        
        guard let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String else{
        
           return "0.0.0"
        }
    
      return  version
    }

}

// MARK----设置TabBar
extension MainViewController{
    

    ///设置 -----TabBar
    private func setupTabBar(){
        //如果两个仅仅实例化的话那么会完全透明
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        //添加撰写按钮
        tabBar.addSubview(composeButton)
        
        composeButton.addTarget(self, action:"clickComposeButton", forControlEvents:.TouchUpInside)
        
    }


}

extension MainViewController{
    
    //加载控制器
    func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title:"首页", imageName:"tabbar_home")
        addChildViewController(MessageViewController(), title:"消息", imageName:"tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), title:"发现", imageName:"tabbar_discover")
        addChildViewController(ProfileViewController(), title:"我", imageName:"tabbar_profile")
    }
 
    private  func addChildViewController(vc:UIViewController,title:String,imageName:String){
       
       vc.title = title

       vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
      
       vc.tabBarItem.image = UIImage(named: imageName)
        // AlwaysOriginal 渲染模式，就会直接使用平面设计师提供的素材颜色，系统不再加工
       vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
       let  nav = UINavigationController(rootViewController:vc)
        
        addChildViewController(nav)

        }
    
}
